package com.byxy.ssm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.byxy.ssm.mapper.ForumMapper;
import com.byxy.ssm.mapper.TabMapper;
import com.byxy.ssm.mapper.TipMapper;
import com.byxy.ssm.mapper.UserMapper;
import com.byxy.ssm.pojo.Forum;
import com.byxy.ssm.pojo.Tab;
import com.byxy.ssm.pojo.TFIntegration;
import com.byxy.ssm.pojo.Tip;
import com.byxy.ssm.pojo.User;
import com.byxy.ssm.service.TFIntegrationService;
import com.byxy.ssm.service.TipService;
import com.byxy.ssm.vo.TipVO;
@Service
@Transactional
public class TipServiceImpl implements TipService{

	@Resource 
	private TipMapper tipMapper;
	@Resource
	private UserMapper userMapper;
	@Resource 
	private ForumMapper forumMapper;
	@Resource
	private TabMapper tabMapper;
	@Resource
	private TFIntegrationService tabForumService;
	@Override
	public int delete(Integer tipId) {
		// TODO Auto-generated method stub
		return tipMapper.delete(tipId);
	}

	@Override
	public int insert(Tip tip) {
		// TODO Auto-generated method stub
		Date time=new Date();
		tip.setTipModifytime(time);
		tip.setTipPublishtime(time);
		tip.setTipClick(0);
		tip.setTipIsdeleted(0);
		tip.setTipIsknot(0);
		tip.setTipReplies(0);
		return tipMapper.insert(tip);
	}

	@Override
	public TipVO getById(Integer tipId) {
		// TODO Auto-generated method stub
		Tip tip=tipMapper.getById(tipId);
		TipVO tipVO=new TipVO();
		BeanUtils.copyProperties(tip, tipVO);
		User user=userMapper.getById(tip.getUserId());
		if (user.getUserNick()!=null&&!user.getUserNick().equals("")) {
			tipVO.setUserName(user.getUserNick());
		}else {
			tipVO.setUserName(user.getUserName());
		}
		TFIntegration tabForum=tabForumService.getById(tip.getTabForumId());
		Tab tab=tabMapper.getById(tabForum.getTabId());
		Forum forum=forumMapper.getById(tabForum.getForumId());
		
		tipVO.setTabName(tab.getTabName());
		tipVO.setForumName(forum.getForumName());
		return tipVO;
	}
	
	@Override
	public Tip getBy(Integer tipId) {
		// TODO Auto-generated method stub
		Tip tip=tipMapper.getById(tipId);
		return tip;
	}

	@Override
	public List<TipVO> findAll() {
		// TODO Auto-generated method stub
		List<Tip> tip=tipMapper.findAll();
		List<TipVO> tVos=new ArrayList<TipVO>();
		for (Tip tp : tip) {
			TipVO tipVO=new TipVO();
			BeanUtils.copyProperties(tp, tipVO);
			User user=userMapper.getById(tp.getUserId());
			if (user.getUserNick()!=null&&!user.getUserNick().equals("")) {
				tipVO.setUserName(user.getUserNick());
			}else {
				tipVO.setUserName(user.getUserName());
			}
			TFIntegration tabForum=tabForumService.getById(tp.getTabForumId());
			Tab tab=tabMapper.getById(tabForum.getTabId());
			Forum forum=forumMapper.getById(tabForum.getForumId());
			
			tipVO.setTabName(tab.getTabName());
			tipVO.setForumName(forum.getForumName());
			tVos.add(tipVO);
			
		}
		
		return tVos;
	}

	@Override
	public int update(Tip tip) {
		// TODO Auto-generated method stub
		return tipMapper.update(tip);
	}

	@Override
	public List<TipVO> userForum(Integer userId) {
		// TODO Auto-generated method stub
		List<Tip> tip=tipMapper.userForum(userId);
		List<TipVO> tVos=new ArrayList<TipVO>();
		for (Tip tp : tip) {
			TipVO tipVO=new TipVO();
			BeanUtils.copyProperties(tp, tipVO);
			User user=userMapper.getById(tp.getUserId());
			if (user.getUserNick()!=null&&!user.getUserNick().equals("")) {
				tipVO.setUserName(user.getUserNick());
			}else {
				tipVO.setUserName(user.getUserName());
			}
			TFIntegration tabForum=tabForumService.getById(tp.getTabForumId());
			Tab tab=tabMapper.getById(tabForum.getTabId());
			Forum forum=forumMapper.getById(tabForum.getForumId());
			
			tipVO.setTabName(tab.getTabName());
			tipVO.setForumName(forum.getForumName());
			tVos.add(tipVO);
			
		}
		return tVos;
	}

}
